package ltd.hxya.novel.common.utils;

import com.alibaba.fastjson.JSON;

public class ConverterUtils {
    public static String toStr(Object value,String defaultValue) {
        if (null==value){
            return defaultValue;
        }
        if (value instanceof String){
            return (String) value;
        }
        return JSON.toJSONString(value);
    }
    public static void toStr(Object value){
        toStr(value,null);
    }

    public static Integer toInt(Object value,Integer defaultValue){
        if (null==value){
            return defaultValue;
        }
        if (value instanceof Integer){
            return (Integer) value;
        }
        if (value instanceof Number){
            return ((Number)value).intValue();
        }
        if (value instanceof String){
            try {
                return Integer.parseInt((String) value);
            }catch (Exception e){
                return defaultValue;
            }

        }
        return defaultValue;
    }

    public static Integer toInt(Object value){
        return toInt(value,null);
    }
}
